package org.summerframework.core.page;


import org.summerframework.core.base.result.DefaultResultCode;
import org.summerframework.core.exception.WebException;
import org.summerframework.core.util.ConvertUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;


/**
 * @author shichao
 */
@Slf4j
public class PaginationHelper {

    public static <T> PageResult<T> fetchPage(long count, PaginationQuery query, List<T> list) throws WebException {
        long totalPage = 1;

        if (query.getIsPagination()) {
            totalPage = count / query.getPageSize();

            if ((count == 0) || ((count % query.getPageSize()) != 0)) {
                totalPage++;
            }
        }

        if (query.getPageIndex() > totalPage) {
            throw new WebException(DefaultResultCode.ILLEGAL_ARGUMENT,
                    "输入的页码[" + query.getPageIndex() + "]不能大于总页数[" + totalPage + "]");
        }
        PageResult<T> result = new PageResult<>();
        result.setPageSize(query.getPageSize());
        result.setPageIndex(query.getPageIndex());
        result.setStartRow(query.getStartRow());
        result.setTotalItem(count);
        result.setTotalPage(totalPage);
        result.setData(list);
        return result;
    }

    /**
     * 转换
     *
     * @param pageResult 分页
     * @param aClass     转换的类
     * @param <T>        返回的类型
     * @return 分页
     */
    public static <T> PageResult<T> fetchPage(PageResult<?> pageResult, Class<T> aClass) {
        PageResult<T> result = fetchPage(pageResult);

        result.setData(ConvertUtils.copyList(pageResult.getData(), aClass));
        return result;
    }

    /**
     * 转换
     *
     * @param pageResult 分页
     * @param function   转换的方法
     * @param <T>        返回的类型
     * @return 分页
     */
    public static <R, T> PageResult<T> fetchPage(PageResult<R> pageResult, Function<R, T> function) {
        PageResult<T> result = fetchPage(pageResult);

        List<T> list = new ArrayList<>();
        result.setData(list);

        for (R r : pageResult.getData()) {
            list.add(function.apply(r));
        }

        return result;
    }

    /**
     * 转换
     *
     * @param pageResult 分页
     * @param <T>        返回的类型
     * @return 分页
     */
    private static <T> PageResult<T> fetchPage(PageResult<?> pageResult) {
        PageResult<T> result = new PageResult<>();
        result.setPageSize(pageResult.getPageSize());
        result.setPageIndex(pageResult.getPageIndex());
        result.setStartRow(pageResult.getStartRow());
        result.setTotalItem(pageResult.getTotalItem());
        result.setTotalPage(pageResult.getTotalPage());
        return result;
    }

    /**
     * 返回空页面
     *
     * @param <T> 类型
     * @return 分页
     */
    public static <T> PageResult<T> emptyPage() {
        PageResult<T> result = new PageResult<>();
        result.setTotalItem(0);
        result.setData(Collections.emptyList());
        return result;
    }
}
